package org.example.lost.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mysql.cj.protocol.x.ReusableInputStream;
import org.example.domain.PageInfoDTO;
import org.example.domain.Result;
import org.example.lost.entity.FoundInfo;
import org.example.lost.entity.LostInfo;
import org.example.lost.entity.vo.HistoryInfoVO;
import org.example.lost.entity.vo.LostInfoVO;
import org.example.lost.mapper.LostMapper;
import org.example.lost.service.LostService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @Author hxy
 * @Date 2021/3/15 10:40
 * @Version 1.0
 */
@Service
public class LostServiceImpl implements LostService {
    @Autowired
    LostMapper lostMapper;


    @Override
    public int addLost(LostInfo lostInfo) {
        lostInfo.setLostId(UUID.randomUUID().toString().replace("-",""));
        lostInfo.setLostReleaseTime(new Date());
        lostInfo.setCreateTime(new Date());
        lostInfo.setLostStatus(0);
        lostInfo.setCheckStatus(0);
        int result = lostMapper.insertLostInfo(lostInfo);
        return result;
    }

    @Override
    public PageInfoDTO<LostInfo> getList(LostInfoVO lostInfoVO) {
        PageHelper.startPage(lostInfoVO.getPageNum(),lostInfoVO.getPageSize());
        List<LostInfo> lostInfoList = lostMapper.selectList(lostInfoVO);
        PageInfo<LostInfo> pageInfo=new PageInfo<>(lostInfoList);
        return PageInfoDTO.to(pageInfo);
    }

    @Override
    public int deleteLost(String lostId) {
        int result = lostMapper.deleteByPrimaryKey(lostId);
        return result;
    }

    @Override
    public int updateLost(LostInfo lostInfo) {
        int result = lostMapper.updateByPrimaryKey(lostInfo);
        return result;
    }

    @Override
    public PageInfoDTO<LostInfo> getOne(HistoryInfoVO historyInfoVO) {
        PageHelper.startPage(historyInfoVO.getPageNum(),historyInfoVO.getPageSize());
        List<LostInfo> lostInfoList = lostMapper.selectByPrimaryKey(historyInfoVO);
        PageInfo<LostInfo> pageInfo = new PageInfo<>(lostInfoList);
        return PageInfoDTO.to(pageInfo);
    }

    @Override
    public String returns(String userId, String lostId) {
        int result =lostMapper.updateReturns(userId,lostId);
        String userPhone = lostMapper.selectUserPhone(lostId);
        if(result == 1){
            return userPhone;
        }
        return "";
    }

    @Override
    public int updateCheckStatus(String lostId, String status) {
        return lostMapper.updateCheckStatus(lostId,status);
    }

}
